package ru.isys.yoomee.ui;

import java.util.ArrayList;

import ru.isys.yoomee.R;
import ru.isys.yoomee.model.Venue;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

@SuppressWarnings("deprecation")
public class VenuesActivity extends Activity
{

    private ArrayList<Venue> mVenues = new ArrayList<Venue>();
    private final static String LOG_TAG = "VenueActivity";
    private ListView mList;
    private static boolean isInDetails = false;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.venues_list_layout);

        mockVenues();

        mList = (ListView) findViewById(android.R.id.list);
        mList.setAdapter(new OrderAdapter(this, R.layout.venues_list_item, mVenues));
    }

    private void mockVenues()
    {
        mVenues.add(new Venue("Groupon", 1102));
        mVenues.add(new Venue("FourSquare", 23));
        mVenues.add(new Venue("Facebook", 1102));
        mVenues.add(new Venue("IBM", 23));
        mVenues.add(new Venue("Starbucks", 1302));
        mVenues.add(new Venue("Apple Store", 456));
        mVenues.add(new Venue("Royal Albert Hall", 1125));
        mVenues.add(new Venue("Mc Donalds", 2223));

    }

    private class OrderAdapter extends ArrayAdapter<Venue>
    {

        private ArrayList<Venue> mListOfVenues;
        private View mView;
        private LayoutInflater mLayoutInflater;
        private Venue mVenue;
        private TextView mNameOfVenue;
        private TextView mDistanceToVenue;
        private Button mCheckinButton;
        private Button mFutureCheckinButton;

        public OrderAdapter(Context context, int textViewResourceId, ArrayList<Venue> items)
        {
            super(context, textViewResourceId, items);
            this.mListOfVenues = items;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            mView = convertView;
            
            

            if (mView == null)
            {
                mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                mView = mLayoutInflater.inflate(R.layout.venues_list_item, null);
            }

            mCheckinButton = (Button) mView.findViewById(R.id.checkin_button);
            mFutureCheckinButton = (Button) mView.findViewById(R.id.future_checkin_button);
            
            mVenue = mListOfVenues.get(position);

            if (mVenue != null)
            {
                mNameOfVenue = (TextView) mView.findViewById(R.id.venue_name);
                mDistanceToVenue = (TextView) mView.findViewById(R.id.distance_to);

                if (mNameOfVenue != null)
                {
                    mNameOfVenue.setText(mVenue.getNameOfVenue());
                }

                if (mDistanceToVenue != null)
                {
                    mDistanceToVenue.setText(mVenue.getDistanceToVenueString());
                }

            }
            
            mCheckinButton.setOnClickListener(new OnClickListener()
            {
                
                @Override
                public void onClick(View v)
                {
                    Log.d("onClick", "Checkin");                    
                }
            });

            mFutureCheckinButton.setOnClickListener(new OnClickListener()
            {
                
                @Override
                public void onClick(View v)
                {
                    Log.d("onClick", "Future check in");
                    
                }
            });                        
            mView.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    Venue venue = mListOfVenues.get(position);
                    Intent intent = new Intent(VenuesActivity.this, VenueDetailsActivity.class);
                    intent.putExtra(VenueDetailsActivity.VENUE, venue);
                    Log.v("FUUUUUU", VenuesTabActivity.group.toString());
                    VenuesTabActivity.group.replaceContentView("VenueDetailsActivity", intent);
                }
            });
            return mView;
        }
    }    
}